﻿using Padsurface.App.Services.Interfaces;
using Padsurface.Audio.Song.Services.Interfaces;
using System;
using System.Collections.ObjectModel;
using System.Composition;
using System.Windows.Input;
using Windows.Storage;

namespace Padsurface.App.ViewModels.Start
{
    [Export]
    public class StartViewModel : BindableBase
    {
        private readonly IFilePicker _filePicker;
        private readonly ISongLoader _songLoader;
        private readonly IActiveSongCache _activeSong;
        private readonly IRecentSongCache _recentSongCache;

        public ObservableCollection<SongProjectViewModel> RecentSongs { get; private set; }

        [ImportingConstructor]
        public StartViewModel(IFilePicker filePicker, ISongLoader songLoader, IActiveSongCache activeSong, IRecentSongCache recentSongCache)
        {
            if (filePicker == null) throw new ArgumentNullException("filePicker");
            if (songLoader == null) throw new ArgumentNullException("songLoader");
            if (activeSong == null) throw new ArgumentNullException("activeSong");
            if (recentSongCache == null) throw new ArgumentNullException("recentSongCache");

            _filePicker = filePicker;
            _songLoader = songLoader;
            _activeSong = activeSong;
            _recentSongCache = recentSongCache;

            RecentSongs = new ObservableCollection<SongProjectViewModel>();
            CreateSongCommand = new DelegateCommand(CreateSong);
            BrowseSongCommand = new DelegateCommand(BrowseSong);
            LoadSongCommand = new DelegateCommand(LoadSong);
        }

        public void Load()
        {
            var recentSongProjects = _recentSongCache.Load();

            foreach (var project in recentSongProjects)
            {
                RecentSongs.Add(project);    
            }
        }

        #region CreateSongCommand

        public ICommand CreateSongCommand { get; private set; }

        private void CreateSong(object obj)
        {
            _activeSong.Clear();

            // Load new song into memory
            // _activeSong.ActiveSong = 

            NavigateToSong();
        }

        #endregion

        #region BrowseSongCommand

        public ICommand BrowseSongCommand { get; private set; }

        private async void BrowseSong(object obj)
        {
            var songFile = await _filePicker.OpenSingleSongFileAsync();

            var isCancelled = (songFile == null);
            if (isCancelled)
                return;

            OpenSong(songFile);
        }

        #endregion

        #region LoadSongCommand

        public ICommand LoadSongCommand { get; private set; }

        private void LoadSong(object obj)
        {
            // pick selected storage file

            // open song
        }

        private async void OpenSong(StorageFile songFile)
        {
            if (songFile == null) throw new ArgumentNullException("songFile");

            var song = await _songLoader.Load(songFile);
            _activeSong.ActiveSong = song;

            _recentSongCache.Add(songFile);

            NavigateToSong();
        }

        #endregion

        private void NavigateToSong()
        {
            Navigate(typeof(Padsurface.App.Views.PadPage).FullName);
        }
    }
}